﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RoomService.Listings
{

    public partial class SearchListings : System.Web.UI.Page
    {
        //RoomSvc.Data.Listing[] listing = RoomService.Service.ServiceClient.GetListings();

        private void Page_Init(Object sender, EventArgs e)
        {
            //SearchBtn.Click += new EventHandler(this.Search_Click);
            //SortBtn.Click += new EventHandler(this.SortBtn_Click);
            //SortBtn.Enabled = false;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataSource3.SelectCommand = "SELECT [listing#] AS column1, [rent], [TotalBeds], [avlBeds], [bathType], [startDate] FROM [Listings]";



        }
        protected void Search_Click(Object sender, EventArgs e)
        /*This method runs all listing through the following tests. 
    The listing that fail the tests are removed from the listing. 
    The listings leftover are added to a new array that will be displayed
    */
        {

            /*
     int ArraySize = listing.Length; //Max Array Size
     for (int i = 0; i < listing.Length; i++)
     {

         if(Smoking_Cb.Checked){
             if (listing[i].smoking == 1) //rids listing array of all rooms where there are smokers
             {
                 listing[i] = null; 
                 ArraySize--; //I decrease the array size of the filtered array.
             }
         }
         if(Furnished_Cb.Checked){

             if ((listing[i]!=null)&&(listing[i].furnished == 0)) 
             {
                 listing[i] = null;
                 ArraySize--;
             }
         }
         if ((listing[i] != null) && (!(withinPriceRange(listing[i].listing_rate.ToString()))))
         {
             listing[i] = null;
             ArraySize--;
         }
      
     }
     int track = 0;
     RoomSvc.Data.Listing[] filter_listing = new RoomSvc.Data.Listing[ArraySize]; //holds the new now filtered listings
        for(int i = 0; i < listing.Length;i++){
            if (listing[i] != null) {
                filter_listing[track] = listing[i];
                track++;
            }
        }
        this.listing = filter_listing;

        postResults(this.listing);
        SortBtn.Enabled = true;
        SortBtn.CssClass = "btn btn-info btn enabled";
     lblMessage.Text = "If you are seeing this the method runs, but you will need to debug in order to see changes in data";



  
        
            
    }
        protected Boolean withinPriceRange(String list_rate)
        {
            if (Convert.ToInt32(this.tb_Max.Text) < (Convert.ToInt32(list_rate)))
            {
                return false;
            }

            if (Convert.ToInt32(this.tb_Min.Text) > (Convert.ToInt32(list_rate)))
            {
                return false;
            }
            return true;
        }

        protected void postResults(RoomSvc.Data.Listing[] listing)
        {
            String[] Rate = new String[listing.Length];
            String[] Sqft = new String[listing.Length];
            String[] Roommates = new String[listing.Length];
            String[] Smoking = new String[listing.Length];
            String[] Furnished = new String[listing.Length];
            String[] DateExp = new String[listing.Length];

            int x = 0;

            for (x = 0; x < listing.Length; x++)
            {
                Rate[x] = listing[x].listing_rate.ToString();
                Sqft[x] = listing[x].square_ft.ToString();
                Roommates[x] = listing[x].roommates.ToString();
                Smoking[x] = listing[x].smoking.ToString();
                Furnished[x] = listing[x].furnished.ToString();
                DateExp[x] = listing[x].date_listing_expire.ToString();
            }

            int rows = listing.Length;
            int cols = 6;
            for (int j = 0; j < rows; j++)
            {
                TableRow r = new TableRow();
                //for (int i = 0; i < cols; i++)
                //{
                TableCell rates = new TableCell();
                rates.Controls.Add(new LiteralControl(Rate[j]));
                r.Cells.Add(rates);

                TableCell sqft = new TableCell();
                sqft.Controls.Add(new LiteralControl(Sqft[j]));
                r.Cells.Add(sqft);

                TableCell room = new TableCell();
                room.Controls.Add(new LiteralControl(Roommates[j]));
                r.Cells.Add(room);

                TableCell smoking = new TableCell();
                smoking.Controls.Add(new LiteralControl(Smoking[j]));
                r.Cells.Add(smoking);

                TableCell furn = new TableCell();
                furn.Controls.Add(new LiteralControl(Furnished[j]));
                r.Cells.Add(furn);

                TableCell dateexp = new TableCell();
                dateexp.Controls.Add(new LiteralControl(DateExp[j]));
                r.Cells.Add(dateexp);
                
                table1.Rows.Add(r);
            }
        }
        

        protected void BubbleSort(RoomSvc.Data.Listing[] ar)
        {
            for (int pass = 1; pass < ar.Length; pass++)
                for (int i = 0; i < ar.Length - 1; i++)
                    if (Convert.ToInt32(ar[i].listing_rate) > Convert.ToInt32(ar[i + 1].listing_rate))
                        Swap(ar, i);
        }
        // swap two items of an array
        protected void Swap(RoomSvc.Data.Listing[] ar, int first)
        {
            RoomSvc.Data.Listing hold;
            hold = ar[first];
            ar[first] = ar[first + 1];
            ar[first + 1] = hold;
        }
        protected void SortBtn_Click(Object sender, EventArgs e) {
            BubbleSort(this.listing);
            postResults(this.listing);
        }
            
        }*/
          
        }
    }
    }

